package com.qijian.mold.domain.param;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.function.Supplier;

/**
 * 分页参数
 * @author anni.zhu
 * @author anni.zhu@king-v.com
 * Created in 2025/3/29 20:12
 */
@Data
public class PageParam {

    /**
     * 当前页
     *
     * @mock 1
     */
    @ApiParam(value = "当前页", defaultValue = "1")
    protected long current = 1;

    /**
     * 每页显示条数
     *
     * @mock 10
     */
    @ApiParam(value = "每页显示条数", defaultValue = "10")
    protected long size = 10;


    /**
     * 排序字段
     */
    private PageSortParam sort;



    public void changeDownLoadPage() {
        this.current = 1;
        this.size = -1L;
    }

    /**
     * 获取分页参数
     * @param target
     * @return
     * @param <T>
     */
    public <T extends Page> T getPageDto(Supplier<T> target) {
        T t = target.get();
        BeanUtils.copyProperties(this, t);

        PageSortParam sortParam = this.sort;
        if (sortParam != null) {
            OrderItem orderItem = new OrderItem();
            BeanUtils.copyProperties(sortParam, orderItem);
            ArrayList<Object> list = new ArrayList<>();
            list.add(orderItem);
            t.setOrders(list);
        }
        return t;
    }


}
